/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import dk.lindhardt.gwt.geie.client.Util;
import dk.lindhardt.gwt.geie.shared.*;

import java.io.Serializable;
import java.util.*;

/**
 * User: AnAmuser
 * Date: 21-08-11
 * <p/>
 * Helper with methods used in the default editing framework.
 */
class ValueEditorFactory {

   /**
    * Returns a value editor for a cell
    * @param editHandler editHandler being used
    * @param cell the cell being edited
    * @return the value editor
    */
   public static ValueEditor getValueEditor(ValueEditor.EditHandler editHandler, Cell cell) {
      ValueEditor valueEditor = null;
      if (cell instanceof LabelCell) {
         valueEditor = new LabelValueEditor(editHandler, cell);
      } else if (cell instanceof DateCell) {
         valueEditor = new DateValueEditor(editHandler, cell, null);
      } else if (cell instanceof NumberCell || cell instanceof NumberFormulaCell) {
         valueEditor = new NumberValueEditor(editHandler, cell);
      } else if (cell instanceof BooleanCell || cell instanceof BooleanFormulaCell) {
         valueEditor = new BooleanValueEditor(editHandler, cell);
      } else if (cell instanceof CellWrapper) {
         Cell wrappedCell = ((CellWrapper) cell).getWrappedCell();
         valueEditor = getValueEditor(editHandler, wrappedCell);
      }

      if (valueEditor != null) {
         valueEditor.setInitialInputValue(cell.getValue());
         return valueEditor;
      }
      throw new RuntimeException("No value editor was found for the cell type " + cell.getClass().getName());
   }

   /**
    * Returns a value editor for a type of cell with the initial value set from the cell
    * @param editHandler editHandler being used
    * @param cell the cell being edited
    * @param type type of the cell
    * @return the value editor
    */
   public static ValueEditor getValueEditorFromTypeText(ValueEditor.EditHandler editHandler, Cell cell, String type, Serializable currentValue) {
      ValueEditor valueEditor = null;
      if (type.equals("Text")) {
         valueEditor = new LabelValueEditor(editHandler, cell);
      } else if (type.equals("Date")) {
         valueEditor = new DateValueEditor(editHandler, cell, null);
      } else if (type.equals("Number")) {
         valueEditor = new NumberValueEditor(editHandler, cell);
      } else if (type.equals("Boolean")) {
         valueEditor = new BooleanValueEditor(editHandler, cell);
      }

      if (valueEditor != null) {
         valueEditor.setInitialInputValue(currentValue);
         return valueEditor;
      }
      throw new RuntimeException("No value editor was found for the cell type " + type);
   }

   public static String getTypeTextFromCell(Cell cell) {
       if (cell instanceof LabelCell) {
         return "Text";
      } else if (cell instanceof DateCell) {
         return "Date";
      } else if (cell instanceof NumberCell || cell instanceof NumberFormulaCell) {
         return "Number";
      } else if (cell instanceof BooleanCell || cell instanceof BooleanFormulaCell) {
         return "Boolean";
      } else if (cell instanceof CellWrapper) {
         return getTypeTextFromCell(((CellWrapper) cell).getWrappedCell());
      }

      throw new RuntimeException("No type text found for the cell type " + cell.getClass().getName());
   }

   public static String[] getPossibleTypes() {
      return new String[] {"Text", "Number", "Date", "Boolean"};
   }
}
